//
//  KDSearchHistoryTool.m
//  PublicSentimentMonitorSystem
//
//  Created by 魏强 on 15/11/23.
//  Copyright © 2015年 魏强. All rights reserved.
//

#import "KDSearchHistoryTool.h"
#import "KDSearchHistory.h"
#import "FMDB.h"
static FMDatabase *_db;

@implementation KDSearchHistoryTool
+ (void)initialize
{
    //1.打开数据库
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"KDSearchHistory.sqlite"];
    _db = [FMDatabase databaseWithPath:path];
    [_db open];
    //2.创表
    [_db executeUpdate:@"CREATE TABLE IF NOT EXISTS t_searchHistory (id integer PRIMARY KEY AUTOINCREMENT, searchHistory blob NOT NULL,searchText text NOT NULL UNIQUE, type integer) ;"];
}
+ (NSArray *)searchHistoryListWithType:(NSInteger )type{
//    KDLog(@"%@", _db.databasePath);

    //根据请求参数生成对应的查询SQL语句
    NSString *sql = [NSString stringWithFormat:@"SELECT *FROM t_searchHistory WHERE type = %ld LIMIT 10", type];
    FMResultSet *set = [_db executeQuery:sql];
    NSMutableArray *tempArr = [NSMutableArray array];
    while (set.next) {
        //NSData ————》NSDictionary
        NSData *data = [set objectForColumnName:@"searchHistory"];
        KDSearchHistory *history = [NSKeyedUnarchiver unarchiveObjectWithData:data];
        [tempArr insertObject:history atIndex:0];
    }
    if (tempArr.count == 0) {//
        return nil;
    }
    return tempArr;
}
/**
 *  获取最后一个搜索记录的ID
 */
+ (NSArray *)search_idsWithType:(NSInteger)type{
    //根据请求参数生成对应的查询SQL语句
    NSString *sql = [NSString stringWithFormat:@"SELECT *FROM t_searchHistory WHERE type = %ld LIMIT 10", type];
    FMResultSet *set = [_db executeQuery:sql];
    NSMutableArray *tempArr = [NSMutableArray array];
    while (set.next) {
        //NSData ————》NSDictionary
         NSNumber *ID = [set objectForColumnName:@"id"] ;
//        KDSearchHistory *history = [NSKeyedUnarchiver unarchiveObjectWithData:data];
        [tempArr addObject:ID];
    }
    if (tempArr.count == 0) {//
        return nil;
    }
    return tempArr;
}
+ (void)saveSearchHistory:(KDSearchHistory *)searchHistory{
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:searchHistory];
    NSArray *arr = [self search_idsWithType:searchHistory.type];
   
    if (arr.count >= 6) {//如果是10个的话，就把第一个替换掉
         NSNumber *LastID = arr[0];
        //先删除最后一个
        [_db executeUpdateWithFormat:@"DELETE FROM t_searchHistory WHERE id = %ld",(long)LastID.integerValue];
        [_db executeUpdateWithFormat:@"replace into t_searchHistory (searchHistory, searchText, type) VALUES (%@, %@, %ld);", data, searchHistory.searchText, (long)searchHistory.type];
    }else{
        [_db executeUpdateWithFormat:@"replace into t_searchHistory (searchHistory, searchText, type) VALUES (%@, %@, %ld);", data,searchHistory.searchText, (long)searchHistory.type];
    }
}

+ (void)deleteHistory:(KDSearchHistory *)history{
    [_db executeUpdateWithFormat:@"DELETE FROM t_searchHistory WHERE searchText = %@ and type = %ld",history.searchText, history.type];
}
@end
